#include <Trade/Trade.h>
#include <Trade/AccountInfo.h>
#include <Trade/SymbolInfo.h>
#include <Trade/PositionInfo.h>
#include <Trade/DealInfo.h>
#include <Trade/TerminalInfo.h>

//-------------------------------------------------------------
// 门面类 - 使用懒加载模式
//-------------------------------------------------------------
class TradeFacade
  {
private:
   // 懒加载指针
   CAccountInfo*     m_account;
   CSymbolInfo*      m_symbol;
   CPositionInfo*    m_position;
   CDealInfo*        m_deal;
   CTrade*           m_trade;
   CTerminalInfo*    m_terminal;

public:
   // 构造函数
                     TradeFacade() :
                     m_account(NULL),
                     m_symbol(NULL),
                     m_position(NULL),
                     m_deal(NULL),
                     m_trade(NULL),
                     m_terminal(NULL)
     {
     }

   // 析构函数 - 释放资源
                    ~TradeFacade()
     {
      SafeDelete(m_account);
      SafeDelete(m_symbol);
      SafeDelete(m_position);
      SafeDelete(m_deal);
      SafeDelete(m_trade);
      SafeDelete(m_terminal);
     }

   // 安全删除指针
   template<typename T>
   void              SafeDelete(T* &ptr)
     {
      if(ptr != NULL)
        {
         delete ptr;
         ptr = NULL;
        }
     }

private:
   // 懒加载方法
   CAccountInfo*     GetAccount()
     {
      if(m_account == NULL)
        {
         m_account = new CAccountInfo();
         Print("CAccountInfo 对象已创建并延迟加载。");
        }
      return m_account;
     }

   CSymbolInfo*      GetSymbol()
     {
      if(m_symbol == NULL)
        {
         m_symbol = new CSymbolInfo();
         Print("CSymbolInfo 对象已创建并延迟加载。");
        }
      return m_symbol;
     }

   CPositionInfo*    GetPosition()
     {
      if(m_position == NULL)
        {
         m_position = new CPositionInfo();
         Print("CPositionInfo 对象已创建并延迟加载。");
        }
      return m_position;
     }

   CDealInfo*        GetDeal()
     {
      if(m_deal == NULL)
        {
         m_deal = new CDealInfo();
         Print("CDealInfo 对象已创建并延迟加载。");
        }
      return m_deal;
     }

   CTrade*           GetTrade()
     {
      if(m_trade == NULL)
        {
         m_trade = new CTrade();
         Print("CTrade 对象已创建并延迟加载。");
        }
      return m_trade;
     }

   CTerminalInfo*    GetTerminal()
     {
      if(m_terminal == NULL)
        {
         m_terminal = new CTerminalInfo();
         Print("CTerminalInfo 对象已创建并延迟加载。");
        }
      return m_terminal;
     }

public:
   // 账户信息接口
   CAccountInfo*     Account() { return GetAccount(); }

   // 交易符号接口
   CSymbolInfo*      Symbol() { return GetSymbol(); }

   // 持仓信息接口
   CPositionInfo*    Position() { return GetPosition(); }

   // 交易历史接口
   CDealInfo*        Deal() { return GetDeal(); }

   // 交易操作接口
   CTrade*           Trade() { return GetTrade(); }

   // 终端信息接口
   CTerminalInfo*    Terminal() { return GetTerminal(); }

   // 打印账户信息（保留原有功能）
   void              PrintAccountInfo()
     {
      CAccountInfo* account = GetAccount();
      Print("账户名: ", account.Name());
      Print("余额: ", DoubleToString(account.Balance(), 2));
      Print("货币: ", account.Currency());
     }
  };
